package org.apache.flink.runtime.metrics.scope;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.MetricOptions;

/** A container for component scope formats. */
public final class ScopeFormats {

    private final ConsumerManagerScopeFormat consumerManagerFormat;
    private final ConsumerManagerConsumerScopeFormat consumerManagerConsumerFormat;
    private final ResoruceProviderScopeFormat taskManagerFormat;
    private final ResourceProviderConsumerScopeFormat taskManagerJobFormat;
    private final ResourceSlotScopeFormat taskFormat;
    // resource consumer
    private final ConsumerScopeFormat resourceConsumerFormat;

    // ------------------------------------------------------------------------

    /** Creates all scope formats, based on the given scope format strings. */
    private ScopeFormats(
            String jobManagerFormat,
            String jobManagerJobFormat,
            String taskManagerFormat,
            String taskManagerJobFormat,
            String taskFormat,
            String resourceConsumerFormat,
            String resourceConsumerResourceFormat) {
        this.consumerManagerFormat = new ConsumerManagerScopeFormat(jobManagerFormat);
        this.consumerManagerConsumerFormat =
                new ConsumerManagerConsumerScopeFormat(jobManagerJobFormat, this.consumerManagerFormat);
        this.taskManagerFormat = new ResoruceProviderScopeFormat(taskManagerFormat);
        this.taskManagerJobFormat =
                new ResourceProviderConsumerScopeFormat(taskManagerJobFormat, this.taskManagerFormat);
        this.taskFormat = new ResourceSlotScopeFormat(taskFormat, this.taskManagerJobFormat);
        // resource consumer
        this.resourceConsumerFormat = new ConsumerScopeFormat(resourceConsumerResourceFormat);
    }

    // ------------------------------------------------------------------------
    //  Accessors
    // ------------------------------------------------------------------------
    public ConsumerScopeFormat getResourceConsuemrFormat() {
        return this.resourceConsumerFormat;
    }
   
    
    public ConsumerManagerScopeFormat getConsumerManagerFormat() {
        return this.consumerManagerFormat;
    }

    public ResoruceProviderScopeFormat getTaskManagerFormat() {
        return this.taskManagerFormat;
    }

    public ResourceProviderConsumerScopeFormat getTaskManagerJobFormat() {
        return this.taskManagerJobFormat;
    }

    public ConsumerManagerConsumerScopeFormat getConsumerManagerConsumerFormat() {
        return this.consumerManagerConsumerFormat;
    }

    public ResourceSlotScopeFormat getTaskFormat() {
        return this.taskFormat;
    }
    
    // ------------------------------------------------------------------------
    //  Parsing from Config
    // ------------------------------------------------------------------------

    /**
     * Creates the scope formats as defined in the given configuration.
     *
     * @param config The configuration that defines the formats
     * @return The ScopeFormats parsed from the configuration
     */
    public static ScopeFormats fromConfig(Configuration config) {
        String jmFormat = config.getString(MetricOptions.SCOPE_NAMING_JM);
        String jmJobFormat = config.getString(MetricOptions.SCOPE_NAMING_JM_JOB);
        String tmFormat = config.getString(MetricOptions.SCOPE_NAMING_TM);
        String tmJobFormat = config.getString(MetricOptions.SCOPE_NAMING_TM_JOB);
        String taskFormat = config.getString(MetricOptions.SCOPE_NAMING_TASK);
        // resource consumer
        String resourceConsumerFormat = config.getString(MetricOptions.SCOPE_NAMING_RC);
        String resourceConsumerResourceFormat = config.getString(MetricOptions.SCOPE_NAMING_RC_RES);

        return new ScopeFormats(
                jmFormat, jmJobFormat, tmFormat, tmJobFormat, taskFormat, resourceConsumerFormat, resourceConsumerResourceFormat);
    }
}
